layui.use(['jquery','layer'],function(){
    var $ = layui.jquery;
    var layer = layui.layer;
    var categroyId = parent.$("#categroyId").val();
    var addChoicespan2 = parent.$("#addChoicespan2").text();
    $("#addChoice").text(addChoicespan2);
    $("#categroyId").val(categroyId);
    var setting = {
        data: {
            simpleData: {
                enable: true,  //true 、 false 分别表示 使用 、 不使用 简单数据模式
                idKey: "categroyId",  //节点数据中保存唯一标识的属性名称
                pIdKey: "parentId",    //节点数据中保存其父节点唯一标识的属性名称
                rootPId: -1  //用于修正根节点父节点数据，即 pIdKey 指定的属性值
            },
            key: {
                name: "title"  //zTree 节点数据保存节点名称的属性名称  默认值："name"
            }
        },
        check:{
            enable: true//开启复选框
        },
        expandSpeed :"fast",

        view:{
            // showLine: false,//样式加线条
            dblClickExpand :false
        },
        callback: {//复选框要选择多个的时候获取树的nodeId是否相同，相同则不设置
            beforeCheck : function(treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId);
                zTree.checkAllNodes(false);
                return true;
            }
        }
    };


    var zTree;
    $(document).ready(function(){
        $.ajax({
            url: '/product/getProductTree',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                zTree = $.fn.zTree.init($("#treeDemo"), setting, data.data);//初始化树
                zTree.expandAll(true);    //true 节点全部展开、false节点收缩
                if($('input[name = categroyId]',window.parent.document).val()){
                    var node = zTree .getNodeByTId("tree_"+$('input[name = categroyId]',window.parent.document).val());
                    zTree.checkNode(node, true, true);
                }
            }
        })
    });
    qvxiao = function qvxiao(){
        var player = parent.layer;
        player.close(player.index);
    }
    queding = function queding() {
        var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = zTreeObj.getCheckedNodes(true);
        var addChoice = "";
        var classification = "";
        var categroyId = "";
        for (var i = 1; i < nodes.length; i++) {
                addChoice += nodes[i].title+ ">";
                classification = nodes[i].title;
                categroyId = nodes[i].categroyId;
        }
        addChoice = addChoice.substring(0,addChoice.length-1);
        parent.$("#addChoicespan1").text("您选择的商品:");
        parent.$("#addChoicespan2").text(addChoice);
        parent.$("#categroyName").val(classification);
        parent.$("#categroyId").val(categroyId);
        var player = parent.layer;
        player.close(player.index);
    }

})